package com.bckj.fastboot.core.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.lang.invoke.SerializedLambda;

@Slf4j
public class LambdaMeta {
    private final SerializedLambda lambda;

    public LambdaMeta(SerializedLambda lambda) {
        this.lambda = lambda;
    }

    public String getFieldName() {
        String methodName = lambda.getImplMethodName();
        String prefix = null;
        if (methodName.startsWith("get")) {
            prefix = "get";
        } else if (methodName.startsWith("is")) {
            prefix = "is";
        }
        if (prefix == null) {
            log.warn("无效的getter方法: " + methodName);
        }
        // 截取get/is之后的字符串并转换首字母为小写（S为diboot项目的字符串工具类，可自行实现）
        assert prefix != null;
        return StringUtils.uncapitalize(methodName.replace(prefix, ""));

    }

    public String getImplClassName() {
        return lambda.getImplClass().replace("/", ".");
    }

}
